package com.gitee.jmliu1983.finacehelper.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

@Aspect
@Component
public class LogExecutionTimeAspect {
    // 拦截所有被 @LogExecutionTime 注解的方法
    @Around("@annotation(logAnnotation)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint, LogExecutionTime logAnnotation) throws Throwable {
        long startTime = System.currentTimeMillis();
        
        // 执行目标方法
        Object result = joinPoint.proceed();

        String jsonStr = JSONArray.toJSONString(joinPoint.getArgs());
        long duration = System.currentTimeMillis() - startTime;
        String methodName = joinPoint.getSignature().toShortString();
        String val = logAnnotation.value();
        System.out.println("方法:" + methodName + ", 参数："+val+", 执行耗时: " + duration + "ms" + ", 方法传参:" +jsonStr + ", 执行结果：" + JSONObject.toJSONString(result));
        return result;
    }
}
